package com.demo.swingquickticket.util;

import com.demo.swingquickticket.SwingQuickTicketApplication;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class ImageUtil {

    public static void base64ToImage(String base64) {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byte[] imageByte = base64Decoder.decodeBuffer(base64);
            String path = SwingQuickTicketApplication.class.getClassLoader().getResource("pic/").getPath();
            path = path + "login.jpg";
            File file = new File(path);
            if (file.exists()) {
                System.out.println("二维码已经存在，执行删除操作");
                file.delete();
            }
            OutputStream outputStream = new FileOutputStream(path);
            outputStream.write(imageByte);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            System.out.println("保存base64图片出现问题");
            e.printStackTrace();
        }
    }
}
